package com.example.word;

import org.docx4j.Docx4J;
import org.docx4j.TraversalUtil;
import org.docx4j.finders.RangeFinder;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.wml.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WordBookmarkExample {

    public static void main(String[] args) throws FileNotFoundException, Docx4JException {
        String basePath = "C:\\Users\\Administrator\\Desktop\\wordtest\\";
        // 设置的标签
        ArrayList<String> bmList = new ArrayList<>();
        bmList.add("username");
        bmList.add("userage");
        bmList.add("usersex");
        // 文件源目录
        String srcPath = basePath + "test.docx";
        // 目标文件目录
        String destPath = basePath + "testResult.docx";
        Map<String, String> map = new HashMap<>();
        map.put("username", "张三");
        map.put("usersex", "男");
        map.put("userage", "27");
        // 打开一个存在的word
        WordprocessingMLPackage wPackage = WordprocessingMLPackage.load(new FileInputStream(srcPath));
        // 作word处理
        WordprocessingMLPackage wPackage1 = replaceContentByBookmark(wPackage, map, bmList);
        wPackage1.save(new File(destPath));
        Docx4J.toPDF(wPackage1, new FileOutputStream(new File(basePath + "test.pdf")));
    }


    public static WordprocessingMLPackage replaceContentByBookmark(WordprocessingMLPackage wPackage, Map<String, String> map, ArrayList<String> bmList) {
        try {
            // 提取正文
            MainDocumentPart main = wPackage.getMainDocumentPart();
            Document doc = main.getContents();
            Body body = doc.getBody();
            // 获取段落
            List<Object> paragraphs = body.getContent();
            // 提取书签并获取书签的游标
            RangeFinder rt = new RangeFinder();
            new TraversalUtil(paragraphs, rt);
            // 遍历书签
            for (CTBookmark bm : rt.getStarts()) {
                // 替换文本内容
                for (String bmName : bmList) {
                    if (bm.getName().equals(bmName)) {
                        Tool.replaceText(bm, map.get(bm.getName()));
                    }
                }
                // 替换image
                if (bm.getName().equals("userimg")) {
                    Tool.addImage(wPackage, bm, "C:\\Users\\0253000023\\Desktop\\4.jpg");
                }
                // 替换image
                if (bm.getName().equals("userimg2")) {
                    Tool.addImage(wPackage, bm, "C:\\Users\\0253000023\\Desktop\\333.jpg");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return wPackage;
    }
}
